﻿using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using TShooter.TfsWord.TeamFoundation;
using TShooter.TfsWord.Win.UI;
using Document = Microsoft.Office.Interop.Word.Document;

namespace TShooter.TfsWord.AddIn.RibbonActions
{
    internal class StartNewDocument: IRibbonAction    
    {
        public void Execute(Window currentWordWindow)
        {
            var picker = new ProjectPicker();
            ConnectionInfo connectionInfo = picker.GetProject();

            if (connectionInfo == null)
            {
                return;
            }

            DocumentCreationDialog.DocumentCreationDialogResult result;
            IEnumerable<int> selectedWorkItems;

            using (var dlg = new DocumentCreationDialog(connectionInfo))
            {
                result = dlg.ShowDialog();
            }

            if (result.DialogResult == DialogResult.OK)
            {
                var tfsConnection = ConnectionFactory.GetConnection(connectionInfo);
                var workItems = new List<string>();

                if (result.CreationMode == DocumentCreationMode.FromExistingWorkItems)
                {
                    foreach (var selectedWorkItem in result.SelectedWorkItems)
                    {
                        workItems.Add(tfsConnection.GetWorkItem(selectedWorkItem));
                    }
                }
                else
                {
                    workItems.Add(tfsConnection.GetBlankWorkItem(result.WorkItemTypeDefinition));
                }
            }
        }
    }
}
